package com.vterm4aKafka.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;

/**
 * <p>Title: utils </p>
 * <p>Description：
 * 请求返回数据处理
 * </p>
 * Package: com.travelsky.tubavterm.utils
 * Copyright: Copyright(c) 2021 Travelsky All Rights Reserved
 * Company: Travelsky
 * Software: TUBA
 * Date: 2021/02/01 14:30
 * @author jiangjie
 * @version 1.0.0
 */
@Data
public class JsonResponse implements Serializable {

    /**
     * @fieldName: serialVersionUID
     * @fieldType: long
     * @Description: 序列号
     */
    private static final long serialVersionUID = -7941354869234545831L;
    // 默认请求成功
    public static final String SUCCESS_CODE = "200";
    public static final String SUCCESS_MESSAGE = "请求成功";

    public static final String FAIL_CODE = "004";
    public static final String FAIL_CODE_2 = "400";
    public static final String FAIL_MESSAGE = "请求失败";

    // code码
    private String code;
    // 返回的信息
    private String msg;
    // 返回的数据
    private Object data;

    /**
     * @Title:JsonResponse
     * @Description:JsonResponse
     * @param code 返回的code码
     * @param msg 返回的信息
     * @param data 返回的数据
     * @author: jiangjie
     * @date: 2020-08-19 14:18:22
     */
    private JsonResponse(String code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        if (null != data) {
            this.data = data;
        } else {
            this.data = new HashMap<String, Object>();
        }
    }

    /**
     * @Title: respSuccess
     * @Description: 返回成功
     * @param data 返回的数据
     * @return: JsonResponse
     * @author: jiangjie
     * @date: 2020-08-19 14:17:38
     */
    public static JsonResponse respSuccess(Object data) {
        return new JsonResponse(SUCCESS_CODE, SUCCESS_MESSAGE, data);
    }

    /**
     * @Title: respSuccess
     * @Description: 返回成功
     * @return: JsonResponse
     * @author: jiangjie
     * @date: 2020-08-19 14:08:37
     */
    public static JsonResponse respSuccess() {
        return new JsonResponse(SUCCESS_CODE, SUCCESS_MESSAGE, null);
    }

    /**
     * @Title: respSuccess
     * @Description: 返回成功
     * @param msg 返回的信息
     * @param data 返回的数据
     * @return: JsonResponse
     * @author: jiangjie
     * @date: 2020-08-19 14:13:48
     */
    public static JsonResponse respSuccess(String msg, Object data) {
        return new JsonResponse(SUCCESS_CODE, msg, data);
    }

    /**
     * @Title: respSuccess
     * @Description: 返回成功
     * @param code 返回的code码
     * @param msg 返回的信息
     * @param data 返回的数据
     * @return: JsonResponse
     * @author: jiangjie
     * @date: 2020-08-19 14:13:48
     */
    public static JsonResponse respSuccess(String code, String msg, Object data) {
        return new JsonResponse(code, msg, data);
    }

    /**
     * @Title: responseFail
     * @Description: 失败不带参数
     * @param code
     * @param message
     * @return
     * @return: JsonResponse
     * @author: jiang_jie
     * @date: 2020-11-13 10:48:01
     */
    public static JsonResponse responseFail(String code, String message) {
        return new JsonResponse(code, message, null);
    }

    public static JsonResponse responseFail(String message) {
        return new JsonResponse(FAIL_CODE, FAIL_MESSAGE, message);
    }

    public static JsonResponse responseFail2(String message) {
        return new JsonResponse(FAIL_CODE_2, FAIL_MESSAGE, message);
    }

    public String toJsonString() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", code);
        jsonObject.put("msg", msg);
        jsonObject.put("data", data);
        return jsonObject.toJSONString();
    }

}
